import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;
//基于NIO非阻塞的简易聊天室 --- 客户端
public class SelectorDemo_client {
    public static void main(String[] args) throws IOException {
        //1.获取通道
        SocketChannel socketChannel = SocketChannel.open();
        //2.连接服务器
        socketChannel.connect(new InetSocketAddress("192.168.191.1",9999) );
        //3.设置非阻塞模式
        socketChannel.configureBlocking(false);
        //4.获取buffer,并写数据到buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        Scanner scanner = new Scanner(System.in);
        while(true){
            System.out.println("Please Input : ");
            String line = scanner.nextLine();
            buffer.put(("Jack: " + line).getBytes());
            buffer.flip();
            socketChannel.write(buffer);
            buffer.clear();
        }
    }
}








//        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//     //   while(true){
//            System.out.println("Please input : ");
//            String line;
//            while((line = reader.readLine() )!= null){
//                if(line.equals("bye"))
//                    break;
//                buffer.put(line.getBytes());
//                buffer.flip();
//
//                //5.发送到服务器端
//                socketChannel.write(buffer);
//                buffer.clear();
//            }
        //      }
